<template>

  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: 16 条</span>
        </template>

        <template #right>
          <el-button type="warning" size="small">excel导入</el-button>
          <el-button type="danger" size="small">excel导出</el-button>
          <el-button type="primary" size="small" @click="isShow = true" @add-employee="getUserList">新增员工</el-button>
        </template>
      </page-tools>

      <el-card v-loading="loading" style="margin-top: 10px;">
        <el-table :data="list" border>
          <el-table-column label="序号" type="index" :index="indexFn" sortable="" width="100" />
          <el-table-column label="姓名" prop="username" sortable="" />
          <el-table-column label="头像" sortable="">
            <template #default="{ row }">
              <img
                v-imgerror="defaultImage"
                class="staff"
                :src="row.staffPhoto || defaultImage"
                alt=""
                @click="show2CodeDialog(row.staffPhoto)"
              >
            </template>
          </el-table-column>
          <el-table-column label="手机号" prop="mobile" sortable="" />
          <el-table-column label="工号" prop="workNumber" sortable="" />
          <el-table-column label="聘用形式" prop="formOfEmployment" :formatter="formatterFn" sortable="" />
          <el-table-column label="部门" prop="departmentName" sortable="" />
          <el-table-column label="入职时间" sortable="">
            <template #default="{ row }">
              {{ row.timeOfEntry | formatTime }}
            </template>
          </el-table-column>
          <el-table-column label="操作" sortable="" fixed="right" width="280">
            <template #default="{row}">
              <el-button type="text" size="small" @click="$router.push(`/employees/detail/${row.id}`)">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small" @click="editRole(row.id)">角色</el-button>
              <el-button type="text" size="small" @click="del(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页组件 -->
        <div style="height: 60px; margin-top: 10px">
          <el-pagination
            :current-page="page"
            :page-sizes="[1,2,3,4,5,6,7,8,9,10]"
            :page-size="size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />

        </div>
      </el-card>

      <add-employee :show-dialog.sync="isShow" />

      <!-- 分享展示, 预览的二维码的弹层 -->
      <el-dialog title="二维码" :visible="showCodeDialog" @close="showCodeDialog=false">
        <el-row type="flex" justify="center">
          <canvas ref="myCanvas" />
        </el-row>
      </el-dialog>

      <AssignRole :show-role-dialog.sync="showRoleDialog" :user-id="userId" />
    </div>
  </div>

</template>

<script>
import AssignRole from './components/AssignRole.vue'

import QrCode from 'qrcode'
import obj from '@/constant/employees'
const { hireType } = obj
import { reqDelEmployee, reqGetUserList } from '@/api/employees'
import AddEmployee from './components/AddEmployee.vue'
export default {
  name: 'Employees',
  components: { AddEmployee, AssignRole },
  data() {
    return {
      page: 1, // 当前页
      size: 5, // 每页几条
      list: [],
      total: 0, // 总数
      loading: false,
      isShow: false,
      defaultImage: 'https://dss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2146034403,1504718527&fm=26&gp=0.jpg',
      showCodeDialog: false,
      showRoleDialog: false,
      userId: ''
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    async getUserList() {
      this.loading = true

      const { data: { rows, total }} = await reqGetUserList(this.page, this.size)
      // console.log(res)
      this.list = rows
      this.total = total
      this.loading = false
    },
    handleSizeChange(value) {
      // 每页几条
      this.size = value
      this.page = 1
      this.getUserList()
    },
    handleCurrentChange(value) {
      // 目前哪条
      this.page = value
      this.getUserList()
    },
    indexFn(val) {
      return val + 1 + (this.page - 1) * this.size
    },
    formatterFn(row, column, cellValue, index) {
      const res = hireType.find(item => item.id === +cellValue)
      return res ? res.value : '未知'
    },
    del(id) {
      this.$confirm('确定删除该员工吗', '提示').then(async() => {
        const res = await reqDelEmployee(id)
        console.log(res, 1111)
        this.$message.success(res.message)
        if (this.list.length === 1 && this.page > 1) {
          this.page--
        }
        this.getUserList()
      }).catch(() => {
        console.log('取消')
      })
    },
    show2CodeDialog(imgURL) {
      console.log(imgURL)
      if (!imgURL) return
      this.showCodeDialog = true
      this.$nextTick(() => {
        // 如果这里 url 写的是网址, 就会跳转到对应网址 (二维码分享效果)
        QrCode.toCanvas(this.$refs.myCanvas, imgURL)
      })
    },
    editRole(id) {
      this.userId = id
      this.showRoleDialog = true
    }
  }
}
</script>

<style lang="scss" scoped>
.employees-container {
  .staff {
    width: 70px;
    height: 70px;
    border-radius: 50%;
  }
}
</style>
